//
//  ZZStatusViewModel.swift
//  web
//
//  Created by 张卓 on 2017/2/4.
//  Copyright © 2017年 ZZ. All rights reserved.
//

import UIKit

class ZZStatusViewModel: NSObject {
    
    // homeModel
    var homeModel: ZZHomeModel?{
        didSet{
            // 处理会员等级逻辑
            membershipImage = dealMembershipImage(mbrank: homeModel?.user?.mbrank ?? 0)
            // 处理用户认证类型
            avatarImage = dealAvatarImage(verified: homeModel?.user?.verified ?? -1)
            // 处理微博来源
            sourceStr = dealSinaSource(source: homeModel?.source)
        }
    }
    
    // 会员等级iamge
    var membershipImage: UIImage?
    // 用户认证类型
    var avatarImage: UIImage?
    // 微博来源字符串
    var sourceStr: String?
    // 微博发布时间
    var sinaTimeStr: String?{
        return dealSinTime(createAt: homeModel?.created_at)
    }
}

//MARK:- 处理用户相关逻辑
extension ZZStatusViewModel{
    /// 会员等级 1-6
    func dealMembershipImage(mbrank: Int) -> UIImage? {
        if mbrank >= 1 && mbrank <= 6{
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }else {
            return UIImage(named: "common_icon_membership")
        }
    }
    
    /// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    func dealAvatarImage(verified: Int) -> UIImage? {
        switch verified {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
        
    }
    // 处理微博来源
    func dealSinaSource(source : String?) -> String{
        //判断source是否为nil
        guard let s = source, s.contains("\">") else {
            return "来自微博"
        }
        //获取起始光标
        let start = s.range(of: "\">")
        // 获取结束光标
        let end = s.range(of: "</a")
        //得到最终的结果范围
        let result = s.substring(with: start!.upperBound..<end!.lowerBound)
        return "来自" + result
    }
    
    //处理微博时间
    func dealSinTime(createAt: Date?) -> String?{
        // 判断createAt是否为niu
        guard let sinaDate = createAt else {
            return nil
        }
        // 时间格式化
        let df = DateFormatter()
        //计算是否为 今年
        let isThisYear = sinDatrisThisYear(sinaDate: sinaDate)
        //是今年
        if isThisYear{
            //日历类
            let calendar = Calendar.current
            //今天
            if calendar.isDateInToday(sinaDate){
                //计算出当前时间和微博日期的秒数差值
                let s = abs(sinaDate.timeIntervalSinceNow)
                //刚刚
                if s <= 60 {
                    return "刚刚"
                }else if s > 60 && s <= 60*60{
                    //XX分钟前  s/60
                    return "\(Int(s/60))分钟前"
                }else{
                    //XX小时前 s/ 60*60
                    return "\(Int(s/(60*60)))小时前"
                }
                
            }else if calendar.isDateInYesterday(sinaDate){
                //昨天
                df.dateFormat = "昨天 HH:mm"
                return df.string(from: sinaDate)
            }else{
                //其他
                // MM-dd HH:mm
                df.dateFormat = "MM-dd HH:mm"
                return df.string(from: sinaDate)
            }
            
            
        }else{
            // 不是今年
            df.dateFormat = "yyyy-MM-dd HH:mm"
            return df.string(from: sinaDate)
        }
    }
    // 判断微博的日期是否为 今年
    func sinDatrisThisYear(sinaDate: Date) -> Bool{
        // 获取当前的日期
        let currentDate = Date()
        //时间格式化
        let df = DateFormatter()
        //制定个事
        df.dateFormat = "yyyy"
        //通过当前的日期得到日期年的字符串
        let currentSte = df.string(from: currentDate)
        //获取新浪微博的日期年的字符串
        let sinaStr = df.string(from: sinaDate)
        return sinaStr == currentSte
    }
    
}
